package com.kee.example.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * Created with IntelliJ IDEA.
 * User: keerthi
 * Date: 12/8/12  6:16 PM
 */
public class HibernateUtil {
    private static final SessionFactory SESSION_FACTORY = buildSessionFactory();


    private static SessionFactory buildSessionFactory() {
        try {
            return new Configuration().configure().buildSessionFactory();
        } catch (Throwable throwable) {
            System.err.println("Error when initializing " + throwable);
            throw new ExceptionInInitializerError(throwable);
        }
    }

    public static SessionFactory getSessionFactory() {
        return SESSION_FACTORY;

    }

    public static void save(SessionFactory inSessionFactory, Object inObject){
        inSessionFactory.getCurrentSession().beginTransaction();
       inSessionFactory.getCurrentSession().persist(inObject);
        inSessionFactory.getCurrentSession().getTransaction().commit();
    }

    public static void delete(SessionFactory inSessionFactory, Object inObject){
        inSessionFactory.getCurrentSession().beginTransaction();
        inSessionFactory.getCurrentSession().delete(inObject);
        inSessionFactory.getCurrentSession().getTransaction().commit();
    }

}
